﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Common.Systems.Memory;
using System.IO;
using System.Drawing.Imaging;
using System.Windows.Media.Imaging;

namespace Common.Graph
{
    class BitmapHandle
    {

        /**
           * \brief 将数张宽高相同的图片按照先上后下、先左后右的顺序进行拼接
           * \param[in] bitmapGroupList 待处理图片数据组
           * \param[in] savePath 图片保存路径
           * \param[in] format 图片保存格式
           * \retval void 空
           */
        public static void AssemblyBitmap(List<List<Bitmap>> bitmapGroupList, string savePath, string format = "Jpeg")
        {
            // 创建空白图层，用于图片拼接
            double width = bitmapGroupList[0][0].Width * bitmapGroupList.Count;
            double height = bitmapGroupList[0][0].Height * bitmapGroupList[0].Count;
            Bitmap bmp = new Bitmap((int)width, (int)height);
            Graphics graphics = Graphics.FromImage(bmp);
            graphics.Clear(Color.Black);

            for (int i = 0; i < bitmapGroupList.Count; i++)
            {
                for (int j = 0; j < bitmapGroupList[i].Count; j++)
                {
                    Bitmap bitmap = bitmapGroupList[i][j];
                    graphics.DrawImage(bitmap, i * bitmap.Width, j * bitmap.Height, bitmap.Width, bitmap.Height);
                }
                Memory.SetProcessMemoryToMin();
            }
            // 保存最终结果图像到指定位置
            using (FileStream stream = new FileStream(savePath, FileMode.Create, FileAccess.ReadWrite))
            {
                switch (format)
                {
                    case "Jpeg":
                        bmp.Save(stream, ImageFormat.Jpeg);
                        break;
                    case "Png":
                        bmp.Save(stream, ImageFormat.Png);
                        break;
                    case "Bmp":
                        bmp.Save(stream, ImageFormat.Bmp);
                        break;
                    case "Gif":
                        bmp.Save(stream, ImageFormat.Gif);
                        break;
                    case "Tiff":
                        bmp.Save(stream, ImageFormat.Tiff);
                        break;
                    case "Wmf":
                        bmp.Save(stream, ImageFormat.Wmf);
                        break;
                    default:
                        bmp.Save(stream, ImageFormat.Jpeg);
                        break;
                }
                stream.Close();
            }
            // 压缩当前进程内存，如果第一次失败，则在一秒后再次进行压缩  2019.09.26 wj 增
            Memory.SetProcessMemoryToMin();
        }

        ///// <summary>
        ///// 将 InkCanvas 笔迹 转化为 BitmapSource 图像
        ///// </summary>
        ///// <param name="canvas"/>InkCanvas 控件
        ///// <returns>存储笔画图像的 BitmapSource</returns>
        //public static BitmapSource ToBitmapSource(System.Windows.Controls.InkCanvas canvas)
        //{
        //    // 获取笔画边界
        //    System.Windows.Rect rect = canvas.Strokes.GetBounds();

        //    // 获取笔画轮廓几何图形
        //    double width = canvas.DefaultDrawingAttributes.Width; // 笔画宽度
        //    double height = canvas.DefaultDrawingAttributes.Height; // 笔画高度
        //    System.Windows.Media.RectangleGeometry geometry = new System.Windows.Media.RectangleGeometry(new System.Windows.Rect(rect.X - width / 2, rect.Y - height / 2, rect.Width + width, rect.Height + height));
        //    canvas.Clip = geometry;
        //    canvas.UpdateLayout();

        //    // 将笔画转换为图像
        //    RenderTargetBitmap bitmap = new RenderTargetBitmap((int)geometry.Rect.Width, (int)geometry.Rect.Height, 96, 96, System.Windows.Media.PixelFormats.Default);
        //    System.Windows.Media.DrawingVisual visual = new System.Windows.Media.DrawingVisual();
        //    using (System.Windows.Media.DrawingContext context = visual.RenderOpen())
        //    {
        //        context.DrawRectangle(new System.Windows.Media.VisualBrush(canvas), null, new System.Windows.Rect(0, 0, geometry.Rect.Width, geometry.Rect.Height));
        //    }
        //    bitmap.Render(visual);
        //    canvas.Clip = null;
        //    return bitmap;
        //}

        ///// <summary>
        ///// 将 Bitmap 转化为 BitmapSource
        ///// </summary>
        ///// <param name="bmp"/>要转换的 Bitmap
        ///// <returns>转换后的 BitmapSource</returns>
        //public static BitmapSource ToBitmapSource(Bitmap bmp)
        //{
        //    System.IntPtr hBitmap = bmp.GetHbitmap();
        //    try
        //    {
        //        return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, System.IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
        //    }
        //    finally
        //    {
        //        DeleteObject(hBitmap);
        //    }
        //}

        ///**
        //   * \brief 将 Bitmap 转化为 BitmapSource
        //   *
        //   * \param bmp 要转换的 Bitmap
        //   * \returns 转换后的 BitmapImage
        //   */
        //public static BitmapImage ToBitmapImages(Bitmap bmp)
        //{
        //    MemoryStream ms = new MemoryStream();
        //    bmp.Save(ms, ImageFormat.Bmp);

        //    BitmapImage image = new BitmapImage();
        //    image.BeginInit();
        //    image.StreamSource = ms;
        //    image.EndInit();
        //    return image;
        //}

        ///**
        //   * \brief 将字节数组转换为 BitmapImage
        //   *
        //   * \param bytes 要转换的字节数组
        //   * \retval 转换后的 BitmapImage
        //   */
        //public static BitmapImage ToBitmapImage(byte[] bytes)
        //{
        //    BitmapImage image = new BitmapImage();
        //    image.BeginInit();
        //    image.StreamSource = new MemoryStream(bytes);
        //    image.EndInit();
        //    return image;
        //}

        /**
           * \brief 将 Bitmap 转化为字节数组
           *
           * \param bmp 要转换的 Bitmap
           * \retval 转换后的字节数组
           */
        public static byte[] ToByteArray(Bitmap bmp)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                bmp.Save(ms, ImageFormat.Bmp);
                return ms.ToArray();
            }
        }
  
        /**
           * \brief 将 BitmapSource 转化为 Bitmap
           *
           * \param source 要转换的 BitmapSource
           * \retval 转化后的 Bitmap
           */
        public static Bitmap ToBitmap(BitmapSource source)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                BitmapEncoder encoder = new BmpBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(source));
                encoder.Save(ms);
                return new Bitmap(ms);
            }
        }
  
        //[System.Runtime.InteropServices.DllImport("Gdi32.dll")]
        //private static extern bool DeleteObject(System.IntPtr hObject);
    }
}
